﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SiteUtils;
using WebComponents.Data;
using WebComponents;

public partial class Module_News : System.Web.UI.UserControl {
  protected void Page_Load(object sender, EventArgs e) {
    //this.Page.Title = WebComponents.ResourceValueController.GetString("Site_News");
    var list = ArticleController.GetByCateID(78, Global.CurrentCultureCode).Where(a => a.IsApproved == true).ToList();
    if (list.Count > 0) {
      CollectionPager pagerNews = new CollectionPager();
      pagerNews.MaxPages = 100;
      pagerNews.PageSize = Global.PageSize("NewsPageSize");
      pagerNews.DataSource = list;
      int page = ConvertType.ToInt(Request.Params["p"]);
      if (page > 0)
        page -= 1;
      pagerNews.DataSourcePaged.CurrentPageIndex = page;
      pagerNews.BindToControl = rptArticle;
      rptArticle.DataSource = pagerNews.DataSourcePaged;
      rptArticle.DataBind();
      if (pagerNews.PageCount > 1) {
        lblPager.Text = "";
        lblPager.Text += string.Format("<a href=\"{1}tin-tuc/p-{0}.gbit\" class='linkpage'><<</a>", 1, Page.ResolveClientUrl("~/"));
        for (int i = 0; i < pagerNews.PageCount; i++) {
          if (i == page)
            lblPager.Text += string.Format("<a href=\"{1}tin-tuc/p-{0}.gbit\" class='linkpage acitve'>{0}</a>", i + 1, Page.ResolveClientUrl("~/"));
          else
            lblPager.Text += string.Format("<a href=\"{1}tin-tuc/p-{0}.gbit\" class='linkpage'>{0}</a>", i + 1, Page.ResolveClientUrl("~/"));
        }
        lblPager.Text += string.Format("<a href=\"{1}tin-tuc/p-{0}.gbit\" class='linkpage'>>></a>", pagerNews.PageCount, Page.ResolveClientUrl("~/"));
      } else
        lblPager.Visible = false;
    }
  }
  protected void rptArticle_ItemDataBound(object sender, RepeaterItemEventArgs e) {
    ArticleInfo article = e.Item.DataItem as ArticleInfo;
    if (article != null) {
      if (article.ImageUrl != "") {
        (e.Item.FindControl("lblImage") as Literal).Text = string.Format("<a href=\"{4}chi-tiet-tin/{0}/{3}.gbit\" class=\"link_pic_icon_news\"><img src=\"{4}{1}\"  width=\"172\" alt=\"{2}\" class=\"pic_border\"/></a>", article.ArticleID, article.ImageUrl, article.Title, ConvertType.StripDiacritics(article.Title), Page.ResolveClientUrl("~/"));
      } else
        (e.Item.FindControl("lblImage") as Literal).Visible = false;
    }
  }
}
